package system_sms_log_dao

import (
	"gitee.com/Luna-CY/Golang-Project-Template/internal/context"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/logger"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/util/pointer"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
	"time"
)

func (cls *SystemSmsLog) SaveSystemSmsLog(ctx context.Context, systemSmsLog *model.SystemSmsLog) errors.Error {
	if nil == systemSmsLog {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("IDSSL_OG.SSL_OG.SSSL_OG.150357 systemSmsLog is nil")

		return errors.ErrorServerInternalError("IDSSL_OG.SSL_OG.SSSL_OG.170357", "异常的传入参数")
	}

	systemSmsLog.UpdateTime = pointer.New(time.Now().Unix())
	if nil == systemSmsLog.CreateTime || 0 == *systemSmsLog.CreateTime {
		systemSmsLog.CreateTime = pointer.New(time.Now().Unix())

		if err := cls.GetDb(ctx).Model(new(model.SystemSmsLog)).Create(&systemSmsLog).Error; nil != err {
			logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("IDSSL_OG.SSL_OG.SSSL_OG.250357 create systemSmsLog failed, err %v", err)

			return errors.ErrorServerInternalError("IDSSL_OG.SSL_OG.SSSL_OG.270357", "创建数据失败")
		}

		return nil
	}

	if err := cls.GetDb(ctx).Model(new(model.SystemSmsLog)).Where("id = ?", systemSmsLog.Id).Updates(&systemSmsLog).Error; nil != err {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("IDSSL_OG.SSL_OG.SSSL_OG.340357 save systemSmsLog failed, err %v", err)

		return errors.ErrorServerInternalError("IDSSL_OG.SSL_OG.SSSL_OG.360357", "更新数据失败")
	}

	return nil
}
